﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace SilverChart.Core.Configuration {
    public class ArrayValueItemHelper<T> : ValueItemHelper<IList<T>> {
        private Boolean isAssigned = false;
        private IList<T> values;
        private ConfigurationItem parentItem;
        private String arrayItemName;
        private String elementName;
        private Boolean createForDefault;

        public ArrayValueItemHelper(ConfigurationItem parentItem, String arrayItemName, String elementName, Boolean createForDefault) {
            this.parentItem = parentItem;
            this.arrayItemName = arrayItemName;
            this.elementName = elementName;
            this.createForDefault = createForDefault;
            values = null;
        }

        public IList<T> Value {
            get {
                if (!isAssigned) {
                    ConfigurationItem item;
                    if (!parentItem.TryGetConfigurationValue<ConfigurationItem>(arrayItemName, out item)){
                        if (createForDefault) {
                            values = new List<T>();
                        }
                    }
                    else if (!item.TryGetConfigurationValues<T>(elementName, out values)) {
                        throw new ArgumentException(elementName + "is not Array item");
                    }
                    isAssigned = true;
                }
                return values;
            }
        }
    }
}
